#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H



#include "../include/global.h"
#include "../include/message.h"
#include "rs.h"

class LBServer;

class FileInfo{
	public:
		FileInfo(unsigned int fileId,unsigned int fileLen);
	public:
		int addNewRS(unsigned int rsId);
		int deleteRS(unsigned int rsId);
	public:
		int getRSSet(vector<unsigned int> & rsset);
		unsigned int getTotalCount();
		void resetCount();
		unsigned int getFileId();
		unsigned int getFileLen();
		void visited();

	private:
		unsigned int _oldCount;
		unsigned int _newCount;
		vector<unsigned int > _rsset;
		pthread_mutex_t count_mutex;

	private:
		unsigned int _fileId;
		unsigned int _fileLen;
};


class ResourceManager{
	public:
		ResourceManager(LBServer *lb,unsigned int resouceNumber,unsigned int maxFileLen,unsigned int minFileLen);
		
		~ResourceManager();
	
	public:
		unsigned int getFileLen(unsigned int fileId);
		void visited(unsigned int fileId);	
		int addNewRS(unsigned int fileId,unsigned int rsId);

		int deleteRS(unsigned int fileId,unsigned int rsId);

		int getRSSet(unsigned int fileId,vector<unsigned int > &rsset);
		unsigned int findMaxCountFile(vector<bool> &canChooseFile);
		unsigned int findBestDest(unsigned int rsId,unsigned int spreadFileId);
	public:
		void initialLayOut(unsigned int rsNumber,unsigned int copys);
		void resetCount(unsigned int fileId);		
		//int requestFile(unsigned int fileId);

	public:
		LBServer *_lb;
		unsigned int _resourceNumber;
		vector<FileInfo*> _fileList;

	private:
		unsigned int _minFileLen;
		unsigned int _maxFileLen;
};

#endif
